CREATE PROCEDURE [HaOnAzure].[ShowEndpoints] AS
SET NOCOUNT ON;

DECLARE @AccountName NVARCHAR(255);
DECLARE @SharedKey NVARCHAR(4000);

SELECT TOP 1 @AccountName = AccountName, @SharedKey = SharedKey 
FROM [HAOnAzure].[Credential] 
WHERE IsMaster = 1;

IF @AccountName IS NULL OR @SharedKey IS NULL
	THROW 53000, 'Default credential not found. Add it and try again.', 1;

SELECT
	Attributes.value('(//CertificateThumbprint)[1]', 'nvarchar(4000)') AS [CertificateThumbprint]
	, Attributes.value('(//SubscriptionId)[1]', 'UNIQUEIDENTIFIER') AS [SubscriptionId]
	, Attributes.value('(//ServiceName)[1]', 'nvarchar(4000)') AS [ServiceName]
	, Attributes.value('(//DeploymentSlot)[1]', 'nvarchar(4000)') AS [DeploymentSlot]
	, PartitionKey AS [DatabaseName]
	, RowKey AS [EndPointName]
	, Attributes.value('(//Port)[1]', 'INT') AS [Port]
	, Attributes.value('(//LocalPort)[1]', 'INT') AS [LocalPort]
	, Attributes.value('(//Protocol)[1]', 'nvarchar(255)') AS [Protocol]
	, Attributes.value('(//VIP)[1]', 'nvarchar(255)') AS [VIP]
	FROM [SQL2AzureCLR].[Azure].QueryTable(
		@AccountName, @SharedKey, 1,
	'HaOnAzureEndpoint', NULL);
GO